package com.sdgakj.service.family;

import com.sdgakj.dto.ActivesDTO;
import com.sdgakj.entity.Actives;
import com.sdgakj.entity.Family;
import com.sdgakj.repository.ActivesDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/26.
 * 加入家族表操作（Actives表）
 */
@Component
@Transactional
public class ActiveService {
    @Autowired
    private ActivesDao activesDao;

    @Autowired
    private FamilyService familyService;
    /**
     * 创建排序.
     */
    private Sort buildSortRequest(String sortName, String sortType) {
        Sort sort = null;
        if ("auto".equals(sortType)) {
            sort = new Sort(Sort.Direction.DESC, "id");
        }else if("desc".equals(sortType)){
            sort = new Sort(Sort.Direction.DESC, sortName);
        }else{
            sort = new Sort(Sort.Direction.ASC, sortName);
        }
        return sort;
    }

    /**
     * 创建分页请求.
     */
    private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
        Sort sort = null;
        if ("auto".equals(sortType)) {
            sort = new Sort(Sort.Direction.DESC, "id");
        }

        return new PageRequest(pageNumber - 1, pagzSize, sort);
    }

    /**
     * 创建动态查询条件组合.
     */
    private Specification<Actives> buildSpecification(Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        //  根据用户类型设置查询条件
//		filters.put("user.type", new SearchFilter("user.type", Operator.EQ, EnumUserType.CUSTOMOR.getCode()));
        Specification<Actives> spec = DynamicSpecifications.bySearchFilter(filters.values(),Actives.class);
        return spec;
    }

    /**
     * 向家族申请表添加申请信息
     * */
    public Actives addActive(Actives active){
        //存的是控制层初始化后的active
        return  activesDao.save(active);
    }
    /**
     * 通过修改状态让该申请记录不显示 需要controller将修改status修改为1
     * */
    public Actives updateActive(Actives active){
        return  activesDao.save(active);
    }
    /**
     * 删除某条申请记录 通过actives的id
     * */
    public void deleteActiveById(Long id){
        activesDao.delete(id);
    }
    /*
    * 查看申请记录详情
    * */
    public Actives getActivesById(Long id) {
        return activesDao.findOne(id);
    }
    /**
     *查询所有申请记录  页面可以通过status字段显示值为 ‘0’的申请记录。
     * */
    public Page<Actives> getActivesByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
                                        String sortType){
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "id");
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Actives> spec = DynamicSpecifications.bySearchFilter(filters.values(),Actives.class);
        return  activesDao.findAll(spec, pageRequest);
    }

    public List<Actives> getActivesByFamilyId(){return  activesDao.getActivesByFamilyId();}

    public List<ActivesDTO> getIdByPersonId(Long pId){
        //通过用户查找到用户的申请记录先
        List<Actives> list=activesDao.getIdByPersonId(pId,0L);
     /*   for(Actives actives:list){
            System.out.println(actives.getId());
        }*/
        //返回的集合
        List<ActivesDTO> activesDTOs=new ArrayList<>();
        //找到所有家族
        List<Family> familyList=familyService.getFamilyAll();
        //遍历 家族的集合，通过家族id去找有没有匹配的申请记录，
        //一个家族判断这个用户有没有加入 通过申请表里边有没有他的记录

        for (Family family:familyList){
            boolean join=false;
            ActivesDTO activesDTO=activesDTO=new ActivesDTO();
            for (Actives actives:list){
                if(family.getId()==actives.getFamily().getId()){
                    join=true;
                }
            }
            activesDTO.setFamily(family);
            activesDTO.setIsJoin(join);
            activesDTOs.add(activesDTO);
        }
        return activesDTOs;
    }
}
